/**
 * Created by woziji00226 on 2024-12-10 13:53:56.
 */
enum EatAppleStatus_W26 {
    Hide,
    Show,
    Eat
}


class GameSprite_eatApple_W26 extends GUI_15001 {
    public static EVENT_EAT_APPLE = 'EVENT_EAT_APPLE';
    public static EVENT_EAT_EMPTY_APPLE = 'EVENT_EAT_EMPTY_APPLE';
    public static EVENT_PLAY_ANIMI_COMPLETED = 'EVENT_PLAY_ANIMI_COMPLETED';

    public static RootHeight = 80;

    private _status: EatAppleStatus_W26 = EatAppleStatus_W26.Hide;
    private _canClick: boolean = false;

    private _backgroundShow = false;
    private _frame = 0;

    private saveClick: boolean = false;

    constructor() {
        super();

        this.status = EatAppleStatus_W26.Hide;

        this.clickRoot.on(EventObject.MOUSE_DOWN, this, this.eatApple);

        this.once(GameSprite.ON_DISPOSE, this, () => {
            this.clickRoot.offAll();
        })
    }


    public playTimeOverAnimi() {
        this._frame = 0;
        os.add_ENTERFRAME(this.timeOverAnimi, this);
    }

    private timeOverAnimi() {
        this._frame += 1;
        if (this._frame % 5 == 0) {
            this.visibleRoot.visible = !this.visibleRoot.visible;
        }

        if (this._frame == 60) {
            this.visibleRoot.visible = true;
            os.remove_ENTERFRAME(this.timeOverAnimi, this);
            EventUtils.happen(GameSprite_eatApple_W26, GameSprite_eatApple_W26.EVENT_PLAY_ANIMI_COMPLETED, [this]);
        }
    }


    /**
     * 播放吃到空的动画
     */
    private playEmptyAnimi() {
        this._frame = 0;
        this._backgroundShow = true;
        os.add_ENTERFRAME(this.emptyAnimi, this);
    }

    /**
     * 空动画
     */
    private emptyAnimi() {
        this._frame += 1;
        if (this._frame % 5 == 0) {
            this._backgroundShow = !this._backgroundShow;
        }

        this.clickRoot.graphics.clear();
        if (this._backgroundShow) {
            this.clickRoot.graphics.drawRect(0, 0, this.clickRoot.width, this.clickRoot.height, "#FF0000");
        }

        if (this._frame == 60) {
            this.clickRoot.graphics.clear();
            this.clickRoot.graphics.drawRect(0, 0, this.clickRoot.width, this.clickRoot.height, "#FF0000");
            os.remove_ENTERFRAME(this.emptyAnimi, this);
            EventUtils.happen(GameSprite_eatApple_W26, GameSprite_eatApple_W26.EVENT_PLAY_ANIMI_COMPLETED, [this]);
        }
    }

    /**
     * 吃苹果
     */
    private eatApple() {
        
        /*
        if (!this.saveClick) {
            this.saveClick = true;
            setTimeout(() => {
                
                this.eatApple();
                this.saveClick = false;
            }, 100);
        }*/

        //debugger;
        if (!this._canClick) {
            return;
        }
  
        if (this._status == EatAppleStatus_W26.Show) {
            this.status = EatAppleStatus_W26.Eat;
            //发出事件吃
            EventUtils.happen(GameSprite_eatApple_W26, GameSprite_eatApple_W26.EVENT_EAT_APPLE, [this]);
        }
        if (this._status == EatAppleStatus_W26.Hide) {
            this.playEmptyAnimi();
            EventUtils.happen(GameSprite_eatApple_W26, GameSprite_eatApple_W26.EVENT_EAT_EMPTY_APPLE, [this]);
        }
    }

    public get status(): EatAppleStatus_W26 {
        return this._status;
    }

    public set status(value: EatAppleStatus_W26) {
        if (value == EatAppleStatus_W26.Eat && this._status == EatAppleStatus_W26.Show) {
            this._status = EatAppleStatus_W26.Eat;

        }

        if (value == EatAppleStatus_W26.Show && this._status == EatAppleStatus_W26.Hide) {
            this._status = EatAppleStatus_W26.Show;
        }


        if (this._status == EatAppleStatus_W26.Hide) {
            this.showRoot.visible = false;
        }

        if (this._status == EatAppleStatus_W26.Show) {
            this.showRoot.visible = true;
            this.eat.visible = false;
            this.default.visible = true;
        }

        if (this._status == EatAppleStatus_W26.Eat) {
            this.showRoot.visible = true;
            this.eat.visible = true;
            this.default.visible = false;
        }
    }

    public get canClick() {
        return this._canClick;
    }

    public set canClick(value: boolean) {
        this._canClick = value;
    }

}